using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

public class ImageController : MonoBehaviour
{
    private GameObject mParentImageTargetImg;
    private GameObject mBtnShowHideImgObj;
    private Text mTextShowHideImg;
    private Image mImage;

    private bool mIsTrackingFound = false;
    private bool mIsShowImg = false;

    void Awake()
    {
        mParentImageTargetImg = GameObject.Find("ImageTargetImg");
        mImage = GameObject.Find("ImageTargetImg/Canvas/Image").GetComponent<Image>();
        var imageControlBtn =
        mBtnShowHideImgObj = GameObject.Find("Canvas/ImageControlButton");
        mBtnShowHideImgObj.GetComponent<Button>().onClick.AddListener(OnBtnShowHideClick); ;
        mTextShowHideImg = mBtnShowHideImgObj.transform.Find("Text").GetComponent<Text>();
        LogUtils.debugGreen("ImageController Awake mBtnShowHideImg =" + mBtnShowHideImgObj);
    }

    void Start()
    {
        LogUtils.debugGreen("ImageController 为 mBtnShowHideImg 添加点击事件成功");
        hideControlBtn();
        hideImg();
    }

    public void OnTrackingFoundInIC()
    {
        LogUtils.debugGreen("--- ImageController OnTrackingFoundInIC");
        mIsTrackingFound = true;
        showControlBtn();
    }

    public void OnTrackingLostInIC()
    {
        LogUtils.debugGreen("--- ImageController OnTrackingLostInIC");
        mIsTrackingFound = false;
        mIsShowImg = false;
        hideControlBtn();
        hideImg();
    }

    public void OnBtnShowHideClick()
    {
        LogUtils.debugGreen("ImageController OnBtnShowHideClick");
        if (mIsTrackingFound)
        {
            ToastUtil.showToast("点击了按钮，并且找到了图片，开始显示/隐藏图片");
            LogUtils.debugGreen("ImageController OnBtnShowHideClick isImgVisible =" + mIsShowImg);

            if (mIsShowImg)
            {
                hideImg();
            }
            else
            {
                showImg();
            }

            mIsShowImg = !mIsShowImg;
        }
        else
        {
            ToastUtil.showToast("点击了按钮，但是没有找到图片");
        }
    }

    private void showControlBtn()
    {
        //SystemUtil.SetGameObjectVisible(mBtnShowHideImgObj);
        mBtnShowHideImgObj.GetComponent<Button>().gameObject.SetActive(true);
    }

    private void hideControlBtn()
    {
        //SystemUtil.SetGameObjectInVisible(mBtnShowHideImgObj);
        mBtnShowHideImgObj.GetComponent<Button>().gameObject.SetActive(false);
    }

    private void showImg()
    {
        LogUtils.debugGreen("ImageController showImg()");
        //SystemUtil.SetGameObjectVisible(mImage.gameObject);
        SystemUtil.SetGameObjectChildrenVisible(mParentImageTargetImg);
        mTextShowHideImg.text = "隐藏图片";
    }

    private void hideImg()
    {
        LogUtils.debugGreen("ImageController hideImg()");
        //SystemUtil.SetGameObjectInVisible(mImage.gameObject);
        SystemUtil.SetGameObjectChildrenInVisible(mParentImageTargetImg);
        mTextShowHideImg.text = "显示图片";
    }

}